/*
 * Package il.ac.biu.cs.grossmm.test.cppToFooAdapter
 * File Publisher.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.fooAdapters;

import static il.ac.biu.cs.grossmm.api.data.NodeOf.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyFactory.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.NodeOf;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.DataProcessorBase;
import il.ac.biu.cs.grossmm.api.flow.NodeExistsException;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;
import il.ac.biu.cs.grossmm.api.flow.PublicationPoint;
import il.ac.biu.cs.grossmm.api.keys.Key;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;
import il.ac.biu.cs.grossmm.api.presence.Entity;
import il.ac.biu.cs.grossmm.api.presence.EntityType;
import il.ac.biu.cs.grossmm.api.presence.Registrar;
import il.ac.biu.cs.grossmm.api.presence.ResourceType;
import il.ac.biu.cs.grossmm.foo.format.Activities;
import il.ac.biu.cs.grossmm.foo.format.Activity;
import il.ac.biu.cs.grossmm.foo.format.Capabilities;
import il.ac.biu.cs.grossmm.foo.format.Mood;
import il.ac.biu.cs.grossmm.foo.format.OnlineStatus;
import il.ac.biu.cs.grossmm.foo.format.PresenceDoc;
import il.ac.biu.cs.grossmm.foo.format.PresenceDoc.Tuple;

public class Publisher extends DataProcessorBase {
	PublicationPoint<NodeOf<String>> pp;
	
	Registrar registrar	;

	public Publisher()
	{
		super("Publisher", true);
	}

	@Override
	public void run()
	{
		try {
			registrar = componentManager.getByClass(Registrar.class);
		} catch (Exception e2) {
			e2.printStackTrace();
			serviceManager.terminated(e2);
			stopped();
			return;
		} 
		
		KeyPattern pattern =
			pattern(Entity.class, 
				mandatory(CONTENT_TYPE, String.class),
				mandatory(RESOURCE_TYPE, ResourceType.class)
			);

		try {
			pp =
				adm.createPublicationPoint(pattern,
					nodeOf(String.class), false, "foo-pub-point");
		} catch (OperationFailedException e) {
			e.printStackTrace();
			serviceManager.terminated(e);
			stopped();
		}
		
		Entity e;
		
		try {
			e = registrar.registerEntity("Alice", EntityType.PRESENTITY);
			registrar.registerEntity("Bob", EntityType.WATCHER);
			
			Entity e2= registrar.getEntity("Bob");
			System.out.println(e2);
			
		} catch (OperationFailedException ee) {
			// TODO Auto-generated catch block
			ee.printStackTrace();
			stopped();
			return;
		}
		
		Key key = key(e, entry(CONTENT_TYPE, "FooFormat"), entry(
			RESOURCE_TYPE, ResourceType.PRESENCE));
		
		Tuple t = new Tuple(
			"123",
			.66f, 
			"me@kitchen.home.com", 
			OnlineStatus.ONLINE, 
			"personal", 
			Capabilities.of("voice", "video")
			);
		
		Tuple t2 = new Tuple(
			"124",
			.1f, 
			"me@kitchen.home.com", 
			OnlineStatus.OFFLINE, 
			"personal", 
			Capabilities.of("voice")
			);
	
		Tuple t3 = new Tuple(
			"129",
			.8f, 
			"me@kitchen.home.com", 
			OnlineStatus.ONLINE, 
			"personal", 
			Capabilities.of("video")
			);
	
		PresenceDoc foo  = new PresenceDoc(
			"Alice", 
				new Mood(1,8,3,6), 
				new Activities(Activity.MEAL, Activity.HOLIDAY),
				"kitchen", 
				"home", 
				t, t2, t3
				);

		try {
			Root<NodeOf<String>> root = pp.createNode(key, false);
			
			NodeManipulator man = pp.writeLock(root);
			
			man.setValue(root, property(String.class), foo.serialize(null).toString());
			
		} catch (NodeExistsException e1) {
			// ok
		} catch (OperationFailedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
